package com.woniu40.petai_user.service;

import com.woniu40.petai_user.entity.Cart;
import com.woniu40.petai_user.mapper.CartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CartService {
    @Autowired
    CartMapper cartMapper;

    public int addCart(Cart cart) {
        cartMapper.insertCart(cart);
        return 1;
    }

    public List<Cart> findCartsByUserId(Integer userId) {
        List<Cart> cartList = cartMapper.selectCartByUserId(userId);
        return cartList;
    }

    public Cart findCartById(Integer cardId) {
        Cart cart = cartMapper.selectCartById(cardId);
        return cart;
    }

    public List<Cart> findCartsByCartIds(List<Integer> cartIds) {
        List<Cart> cartList = cartMapper.selectCartByIds(cartIds);
        return cartList;
    }

    public int modifyCart(Cart cartToUpdate) {
        cartMapper.updateCart(cartToUpdate);
        return 1;
    }


    public int removeBatch(String cartIds) {
        List<Integer> idList = Arrays.stream(cartIds.split(":"))
                .map(s -> Integer.valueOf(s))
                .collect(Collectors.toList());

        cartMapper.deleteBatch(idList);
        return 1;
    }

    public int removeByOrderBatch(Integer orderId) {
        cartMapper.deleteByOrderBatch(orderId);
        return 1;
    }

    public int remove(Integer cartId) {
        cartMapper.deleteCart(cartId);
        return 1;
    }

    public int removeCartAll() {
        cartMapper.deleteCartAll();
        return 1;
    }


    //===========================自己写======================================

    public Cart findCart(Integer cartId){
        return cartMapper.select(cartId);
    }

    public int modifyBuyCount(Integer cartId, Integer buyCount){
        return cartMapper.updateBuyCount(cartId,buyCount);
    }

    public int removeOrder(Integer cartId, Integer userId){
        return cartMapper.deleteOrder(cartId, userId);
    }

    public int removeAllOrder(Integer userId){
        return cartMapper.deleteAllOrder(userId);
    }







}
